<?php

/**f
 * becas actions.
 *
 * @package    auip
 * @subpackage becas
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class becasActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {   
      //$this->datos = $request->getParameter('v');
        $this->becas = new sfDoctrinePager('Beca', 5);
        $this->becas->setQuery(Doctrine::getTable('Beca')->createQuery('b')->where('b.registro_activo <> 0')->addOrderBy('b.fecha_inicio DESC')/*->andWhere( 'b.fecha_inicio >= ?',date('Y-m-d H:i:s', time() + 86400 * sfConfig::get('app_active_days')) )*/ );
        $this->becas->setPage($request->getParameter('pagina', 1));
        $this->becas->init();
  }
  
  public function executeShow(sfWebRequest $request)
  {
    $this->beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('id')));       
    $this->forward404Unless($this->beca);    
  }

  public function executeNew(sfWebRequest $request)
  {       
      $this->form = new BecaForm(); 
      //$this->form
  }
  
  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BecaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }
 
  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('id'))), sprintf('Object beca does not exist (%s).', $request->getParameter('id')));
    $this->form = new BecaForm($beca);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('id'))), sprintf('Object beca does not exist (%s).', $request->getParameter('id')));
    $this->form = new BecaForm($beca);    
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }
  
  public function executeEntidad(sfWebRequest $request)
  {
      $this->formulario = new UniversidadForm();      
  }  
  
  
  public function executeTest(sfWebRequest $request){}
  public function executeProcesador(sfWebRequest $request)
  {    
    if ($request->isXmlHttpRequest()) 
    {        
        $q = Doctrine_Query::create()->from('Beca b')->where('1 = 1');
        
        if ($request->getParameter('parametro0') != '')
        {   
          $q->andWhere('b.nombre ILIKE ?', '%'.$request->getParameter('parametro0').'%');          
        }
        if($request->getParameter('parametro1') != '')
        {        
          $q->andwhere('b.universidad_ofrece_id='.BecaTable::getIdUniversidad($request->getParameter('parametro1')));         
        }      
//        if($request->getParameter('p4') != ' ')
//        { 
//            $q->andwhere('b.fecha_inicio BETWEEN '."'".Mensajero::fechaOut($request->getParameter('p3'))."'".' AND '."'".Mensajero::fechaOut($request->getParameter('p4'))."'");       
//        }      
        $this->becas=$q->execute();        
    }
    //$respuesta = '<persona><nombre>Hernando</nombre><apellido>Vidal</apellido></persona>';    
    //$this->getResponse()->setContent($respuesta); 
    //return sfView::NONE;
}
  
  public function executeDelete(sfWebRequest $request)
  {
//    $request->checkCSRFProtection();
//    $this->forward404Unless($beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('id'))), sprintf('Object beca does not exist (%s).', $request->getParameter('id')));
//    $beca->delete();
    $arr_use = $request->getPostParameter('becas');
    
    for($i=0; $i<count($arr_use); $i++)
    {
        //print '-->'.$arr_use[$i];
        $this->forward404Unless($beca = Doctrine_Core::getTable('Beca')->find($arr_use[$i]), sprintf('El objeto beca NO EXISTE (%s).', $arr_use[$i]));
        $beca->setRegistroActivo(0);
        $beca->save();
    } 
    $this->redirect('becas/index');
  }
  
  
  public function executeUniversi(sfWebRequest $request)
  {
       //$this->nuevo = Doctrine_Core::getTable('Universidad')->createQuery('u')->orderBy('u.pais_id, u.region_id')->execute();
       $this->nuevo = new sfDoctrinePager('Universidad', 20);
       $this->nuevo->setQuery(Doctrine::getTable('Universidad')->createQuery('u')->addOrderBy('u.pais_id, u.region_id'));
       $this->nuevo->setPage($request->getParameter('pagina', 1));
       $this->nuevo->init();
  }
  public function executeCrear(sfWebRequest $request)
  {
      
  }
  public function executeGuardar(sfWebRequest $request)
  {      
      $arreglo[] = $request->getPostParameter('universidades');
      //print_r($arreglo);
      //$becas = new Beca();      
      //echo '---> '.$arreglo;
//      $unaBeca = new Beca();
//      $unaBeca->setLogo($request->getPostParameter('logo'));
//      $unaBeca->setNombre($request->getPostParameter('nombre')  );
//      $unaBeca->setDescripcion($request->getPostParameter('descripcion')  );
//      $unaBeca->setDirigidoA($request->getPostParameter('dirigido')  );
//      $unaBeca->setRequisitos($request->getPostParameter('requisitos')  );
//      $unaBeca->setPlazo($request->getPostParameter('plazo')  );
//      $unaBeca->setFechaInicio($request->getPostParameter('fecha_inicio')  );
//      $unaBeca->setFechaFin($request->getPostParameter('fecha_fin')  );
//      $unaBeca->save();
//      //echo '---> '.$unaBeca->get('id', true);
//      $this->redirect('becas/edit?id='.$unaBeca->get('id', true));
  }  
  /*FUNCION QUE PERMITE REVISAR EL FORMULARIO ANTES DE GUARDARLO*/
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));            
    $valor = $request->getParameter('adicion');
    
    if ($form->isValid())
    {   
       /*$id_beca_guardada = $this->form->getValue('beca_id');
       if ($id_beca_guardada =! 0)
       {
           $this->redirect('becas/update?id='.$id_beca_guardada);
       }
       else
       {*/
            $file = $this->form->getValue('logo');
            $id_uni = $this->form->getValue('universidad_ofrece_id');   
            $id_beca = $this->form->getValue('id');
            $file->setNameFile('logo'.$id_uni);
            $nuevaBeca = Doctrine::getTable('Beca')->createQuery('b')->where('b.logo ILIKE ?', '%logo'.$id.'%')->execute()->toArray();                    
       
            //PERMITE ENCONTRAR SI EL LOGO YA SE ENCUENTRA INGRESADO
            $resultado = count($nuevaBeca);       
            if ($resultado === 1) 
            {
                $file->saveNoFile('logo'.$id.'.'.$file->getExtension());
                if ($valor == 1)
                {   
                    $beca = $form->save();    
                
                    $this->getUser()->setAttribute('beca_id', $beca->getId());
                    $this->getUser()->setAttribute('nom', $beca->getNombre());           
                    $this->redirect('atributos/new');                    
                }
                else
                {           
                    $beca = $form->save();           
                    $this->redirect('becas/index');                    
                }                
             }
             else
             {
                if ($valor == 1)
                {             
                        $beca = $form->save();                      
                        $this->getUser()->setAttribute('beca_id', $beca->getId());
                        $this->getUser()->setAttribute('nom', $beca->getNombre());           
                        $this->redirect('atributos/new');
                }
                else
                {           
                    $beca = $form->save();           
                    $this->redirect('becas/index');        
                }    
              }          
        /*}//FIN ELSE BECA GUARDADA*/
       
     }//FIN SI VALIDATE
    
  }//FIN FUNCION    
    
}
